package server;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;
    private Host[] hosts;

    public RequestProcessor(Socket socket, Host[] hosts) {
        this.socket = socket;
        this.hosts = hosts;
    }

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            for (int i = 0; i < hosts.length; i++) {
                if (hosts[i].process(request, response)) {
                    socket.close();
                    return;
                }
            }

            // 处理啥也没有的情况
            response.output(HttpProtocolUtil.getHttpHeader404());
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
